<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>useCallback</title>
  </head>
  <body>
    <div id="root"></div>
    <script src="../../libs/babel.min.js"></script>
    <script src="../../libs/react-17.0.2.js"></script>
    <script src="../../libs/react-dom-17.0.2.js"></script>
    <script type="text/babel">
      const App = props => {
        // 添加一个 hook
        const [num, setNum] = React.useState(1)
        const [visible, setVisible] = React.useState(true)

        const handleClickBtn = () => {
          setNum(num2 => num2 + 2)
        }
        // 切换visible
        const handleClickChangeVisible = () => {
          setNum(!visible)
        }
        // 打印
        const doSome = React.useCallback(() => {
          console.log("useCallback" + num)
        }, [visible])

        return (
          <div>
            <button onClick={handleClickBtn}>切换Num = {num}</button>
            <button onClick={handleClickChangeVisible}>切换Visible = {visible}</button>
            <button onClick={doSome}>查看doSome打印的内容 </button>
          </div>
        )
      }
      const root = document.getElementById("root")
      ReactDOM.render(<App app={1} />, root)
    </script>
  </body>
</html>
